package edu.xidian.bdilab.jrbigdata.service.bank.ProductApplyInfo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductApplyInfo;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.FinancialProductMapper;
import edu.xidian.bdilab.jrbigdata.service.bank.ProductApplyInfo.ProductApplyInfoService;
import edu.xidian.bdilab.jrbigdata.service.bank.productApplyCheck.ProductApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

/**
 * @author zhangmmm
 * @create 2020-12-04 18:59
 */
@Service
public class ProductApplyInfoServiceImpl implements ProductApplyInfoService {
    @Autowired
    FinancialProductMapper financialProductMapper;
    @Autowired
    ProductApplyService productApplyService;

    @Override
    public ResponseResult getProductApplyInfo(String bankName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<ProductApplyInfo> productApplyInfoList=new ArrayList<>();
        try{
        //获取本银行所有的产品申请记录
        List<ProductRequest> allProductRequest = productApplyService.getProRequest(bankName);
        for (ProductRequest productRequest:allProductRequest) {
            ProductApplyInfo productApplyInfo=new ProductApplyInfo();
            productApplyInfo.setRequestTime(productRequest.getRequestTime());
            productApplyInfo.setFkProductName(productRequest.getFkProductName());
            productApplyInfo.setFkSocialCode(productRequest.getFkSocialCode());
            productApplyInfo.setEnterpriseName(productRequest.getEnterpriseName());
            productApplyInfo.setStatus(productRequest.getStatus());
            productApplyInfo.setComment(productRequest.getComment());
            productApplyInfo.setFundUsage(productRequest.getFundUsage());
            //通过产品名称查询对应的产品信息
            QueryWrapper<FinancialProduct> ProductQueryWrapper = new QueryWrapper<>();
            ProductQueryWrapper.eq("product_name",productRequest.getFkProductName());
            FinancialProduct Product= financialProductMapper.selectOne(ProductQueryWrapper);
            productApplyInfo.setLoanLimit(Product.getLoanLimit());
            productApplyInfo.setLoanPeriod(Product.getLoanPeriod());
            productApplyInfo.setRateUpperLimit(Product.getRateUpperLimit());
            productApplyInfo.setRateLowerLimit(Product.getRateLowerLimit());

            productApplyInfoList.add(productApplyInfo);
        }
        PageInfo pageInfo=new PageInfo<> (productApplyInfoList);
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>(3);
        data.put("productApplyInfoList",productApplyInfoList);
        data.put("Total Page Num",pageInfo.getPages());
        data.put("Total",pageInfo.getTotal());
        if (productApplyInfoList.size() == 0) {
            responseResult.setCode(BankCode.CHECK_NONE.getCode());
            responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
            responseResult.setSuccess(TRUE);
        } else {
            responseResult.setData(data);
            responseResult.setCode(BankCode.GET_PRODUCT_APPLYINFO_SUCCESS.getCode());
            responseResult.setMessage(BankCode.GET_PRODUCT_APPLYINFO_SUCCESS.getMessage());
            responseResult.setSuccess(TRUE);
        }
        return responseResult;
    }catch (Exception e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(BankCode.GET_PRODUCT_APPLYINFO_FAILED.getCode());
        responseResult.setMessage(BankCode.GET_PRODUCT_APPLYINFO_FAILED.getMessage());
        responseResult.setSuccess(FALSE);
        return responseResult;
    }
    }
}
